package farmers.javcap;

import farmers.javcap.bean.ThreadBean;
import farmers.javcap.bean.ThreadUtil;
import farmers.javcap.util.FileUtil;
import farmers.javcap.util.HttpUtil;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Javcap {



    public static void main(String[] args) throws Exception {
        // 设置代理
        HttpUtil.setProxyServer("127.0.0.1", "51081");


        // 创建一个可重用线程数的线程池
//        ExecutorService pool = Executors.newCachedThreadPool();
        ExecutorService pool = Executors.newFixedThreadPool(20);


        // 获取图片url列表
        //
        //
        //
        //        MVSD FJIN  FUGA HMN REAL QXSA PRIN MOOR MKMP DASS
        // CEMD APNS AVSW AARM KAAD OLM SGSR FAYS HODV SDMM SGKI SUJI NIMA MUDR FSTU BLK NVH PKPD KSBJ MBDD ABF FTBD
        //  PRED PPPE MNGS  MIAB SLAP HHF EYS BLOR YAKO  HONB HRSM SKMJ SKMJ DVMM SW BLK TANF PRMJ SUJI
        //  MFYD MOPT REBD MFCD DOCD TIKB ONIN MUKD NCYF  GDJP ERDM GBSA DVMM ASIA MBMP SDJS JKSR BDSR UMSO EVIS KTRA AGMX ODVHJ AKDL
        // FOCS ABF URKK SOAN  FKOU  SSNI FPRE GQN KNMB XVSR

        // 从1开始下载
        //  HUNTC HUNTA SONE JUR CJOD JURA SDNM ROE ALDN MKON RMER HERY EKDV

        // 正在下载
//        String 番号前缀 = "";
        String 番号前缀 = "";

        for (int i = 1; i <= 999; i++) {
            boolean flag = 检查已下载("f:\\vphoto3\\" + 番号前缀 + "\\", 番号前缀 + "-" + String.format("%03d", i));

            if(flag == false){
                continue;
            }
            ThreadBean thread = ThreadUtil.获取url单个("https://javcup.com/movie/" + 番号前缀 + "-", i);
            thread.setFilePath("f:\\vphoto3\\" + 番号前缀 + "\\");  //下载路劲

            String downFileName = "D:\\IdeaProjects\\farmers\\zUtil\\src\\main\\java\\farmers\\javcap\\" + 番号前缀 + ".txt";
            FileUtil.write(downFileName, thread.getFileName() + " " + thread.getImgUrl() + " " + thread.getVideoUrl());

            if(StringUtils.isEmpty(thread.getFileName()) || StringUtils.isEmpty(thread.getImgUrl())){
               continue;
            }
            pool.execute(thread);
        }

        // 关闭线程池
        pool.shutdown();
    }

    // true=下载  false=不下载
    public static boolean 检查已下载(String 路径, String 文件){
        File folder = new File(路径);
        // 列出所有文件和文件夹
        File[] files = folder.listFiles();
        if(files == null){
            return true;
        }
        for (File file : files) {
            if(file.getName().contains(文件)){
                System.out.println("文件已存在 " + 文件);
                return false;
            }
        }
        return true;
    }
}
